local skel = fk.CreateSkill{
  name = "tea__jianzhi",
  tags = { Skill.Compulsory }
}
--local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__jianzhi"] = "坚执",
  [":tea__jianzhi"] = "锁定技，当你每阶段首次使用伤害牌时，你摸一张牌，然后弃置一张牌，若你以此法弃置的牌与你以此法摸的牌：不同，此伤害牌无效，且你本回合不能使用与此伤害牌同名的牌；相同，此阶段结束时，你展示所有手牌并依次使用其中与此伤害牌同名的牌。",
  ["#tea__jianzhi-discard"] = "坚执：你需要弃置一张牌",
  ["@tea__jianzhi-turn"] = "坚执异",
  ["@tea__jianzhi-phase"] = "坚执同",
  ["#tea__jianzhi-use"] = "坚执：你可以使用手牌中的所有【%arg】",
}
---@type TrigSkelSpec<UseCardFunc>
local jianzhi = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player == target and data.card and data.card.is_damage_card then
      local phase_id = player.room.logic:getCurrentEvent():findParent(GameEvent.Phase).id
      if phase_id then
        local events = player.room.logic:getEventsByRule(GameEvent.UseCard, 2, function (e)
          return e.data.card and e.data.from == player and e.data.card.is_damage_card
        end, phase_id)
        return #events == 1
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local draw_card = player:drawCards(1, skel.name)
    if not player.dead and not player:isNude() then
      local cid = room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        include_equip = true,
        prompt = "#tea__jianzhi-discard",
        cancelable = false
      })
      if #cid == 1 then
        room:notifySkillInvoked(player, skel.name, "special")
        if draw_card[1] ~= cid[1] then
          player:broadcastSkillInvoke(skel.name, table.random{2, 3})
          data.nullifiedTargets = data.nullifiedTargets or {}
          for _, p in ipairs(room.players) do
            table.insertIfNeed(data.nullifiedTargets, p)
          end
          room:setPlayerMark(player, "@tea__jianzhi-turn", data.card.trueName)
        else
          player:broadcastSkillInvoke(skel.name, 1)
          room:setPlayerMark(player, "@tea__jianzhi-phase", data.card.trueName)
        end
      end
    end
  end
}
---@type TrigSkelSpec<UseCardFunc>
local jianzhi_phase = {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(skel.name) and player:getMark("@tea__jianzhi-phase") ~= 0 and player:canUse(Fk:cloneCard("slash"), { bypass_times = true })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cids = player:getCardIds("h")
    player:showCards(player:getCardIds("h"))
    if not player.dead then
      while not player.dead and not player:isKongcheng() do
        local cards = table.filter(cids, function (id)
          local c = Fk:getCardById(id)
          return #c:getDefaultTarget(player) > 0 and table.contains(player:getCardIds("h"), id)
        end)
        if #cards == 0 then break end
        local use = room:askToUseRealCard(player, {
          skill_name = skel.name,
          pattern = player:getMark("@tea__jianzhi-phase") .. "|.|.|hand|.|.|" .. table.concat(cids, ","),
          prompt = "#tea__jianzhi-use:::" .. player:getMark("@tea__jianzhi-phase"),
          extra_data = {
            bypass_times = true,
          },
          cancelable = false
        })
        if not use then
          break
        end
      end
    end
  end
}
---@type ProhibitSpec
local jianzhi_prohibit = {
  prohibit_use = function (self, player, card)
    return player:hasSkill(skel.name) and card and card.trueName == player:getMark("@tea__jianzhi-turn")
  end
}
skel:addEffect(fk.CardUsing, jianzhi)
skel:addEffect(fk.EventPhaseEnd, jianzhi_phase)
skel:addEffect("prohibit", jianzhi_prohibit)
-- Fk.generals["mouxusheng"]:addSkill(skel.name)
return skel